package com.xmall.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmall.auth.dao.SysRoleMapper;
import com.xmall.auth.dao.SysRoleMenuRelationMapper;
import com.xmall.auth.dao.SysRolePermissionRelationMapper;
import com.xmall.auth.dao.SysRoleResourceRelationMapper;
import com.xmall.auth.entity.*;
import com.xmall.auth.service.ISysRoleService;
import com.xmall.common.config.PlatformContant;
import com.xmall.common.config.PojoUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 后台用户角色表 服务实现类
 * </p>
 *
 * @author liuzh
 * @since 2020-06-14
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
    @Autowired
    SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRolePermissionRelationMapper sysRolePermissionRelationMapper;
    @Autowired
    private SysRoleMenuRelationMapper sysRoleMenuRelationMapper;
    @Autowired
    private SysRoleResourceRelationMapper sysRoleResourceRelationMapper;

    @Override
    public boolean saveSysRole(SysRole sysRole) {
        PojoUtils.setSysProperties(sysRole, PlatformContant.OpType.insert);
        return save(sysRole);
    }

    @Override
    public boolean updateSysRole(Long id, SysRole sysRole) {
        SysRole oldSysRole = getById(id);
        BeanUtils.copyProperties(sysRole, oldSysRole);
        PojoUtils.setSysProperties(oldSysRole, PlatformContant.OpType.update);
        boolean update = update(
                oldSysRole,
                new QueryWrapper<SysRole>()
                        .lambda().eq(SysRole::getId, id)
        );
        return update;
    }

    @Override
    public boolean deleteSysRole(Long id) {
        boolean b = removeById(id);
        return b;
    }

    @Override
    public int deleteSysRoleIds(List<Long> ids) {
        return sysRoleMapper.deleteBatchIds(ids);
    }

    @Override
    public SysRole getSysRole(Long id) {
        SysRole sysRole = getById(id);
        return sysRole;
    }

    /**
     * 获取指定角色权限
     *
     * @param roleId
     */
    @Override
    public List<SysPermission> getPermissionList(Long roleId) {
        return sysRolePermissionRelationMapper.getPermissionList(roleId);
    }

    /**
     * 修改指定角色的权限
     *
     * @param roleId
     * @param permissionIds
     */
    @Override
    public int updatePermission(Long roleId, List<Long> permissionIds) {
        //先删除原有关系
        sysRolePermissionRelationMapper.delete(new QueryWrapper<SysRolePermissionRelation>().lambda().eq(SysRolePermissionRelation::getRoleId, roleId));
        //批量插入新关系
        List<SysRolePermissionRelation> relationList = new ArrayList<>();
        for (Long permissionId : permissionIds) {
            SysRolePermissionRelation relation = new SysRolePermissionRelation();
            relation.setRoleId(roleId);
            relation.setPermissionId(permissionId);
            PojoUtils.setSysProperties(relation, PlatformContant.OpType.insert);
            sysRolePermissionRelationMapper.insert(relation);
        }
        return relationList.size();
    }


    @Override
    public List<SysRole> list() {
        return sysRoleMapper.selectList(new QueryWrapper<SysRole>());
    }

    /**
     * 根据管理员ID获取对应菜单
     *
     * @param adminId
     */
    @Override
    public List<SysMenu> getMenuList(Long adminId) {
        return sysRoleMapper.getMenuList(adminId);
    }

    @Override
    public List<SysMenu> listMenu(Long roleId) {
        return sysRoleMapper.getMenuListByRoleId(roleId);
    }

    @Override
    public List<SysResource> listResource(Long roleId) {
        return sysRoleMapper.getResourceListByRoleId(roleId);
    }

    @Override
    public int allocMenu(Long roleId, List<Long> menuIds) {
        //先删除原有关系
        sysRoleMenuRelationMapper.delete(new QueryWrapper<SysRoleMenuRelation>().lambda().eq(SysRoleMenuRelation::getRoleId, roleId));
        //批量插入新关系
        for (Long menuId : menuIds) {
            SysRoleMenuRelation relation = new SysRoleMenuRelation();
            relation.setRoleId(roleId);
            relation.setMenuId(menuId);
            PojoUtils.setSysProperties(relation, PlatformContant.OpType.insert);
            sysRoleMenuRelationMapper.insert(relation);
        }
        return menuIds.size();
    }

    @Override
    public int allocResource(Long roleId, List<Long> resourceIds) {
        //先删除原有关系
        sysRoleResourceRelationMapper.delete(new QueryWrapper<SysRoleResourceRelation>().lambda().eq(SysRoleResourceRelation::getRoleId, roleId));
        //批量插入新关系
        for (Long resourceId : resourceIds) {
            SysRoleResourceRelation relation = new SysRoleResourceRelation();
            relation.setRoleId(roleId);
            relation.setResourceId(resourceId);
            PojoUtils.setSysProperties(relation, PlatformContant.OpType.insert);
            sysRoleResourceRelationMapper.insert(relation);
        }
        return resourceIds.size();
    }
}
